/**
 * Created by wenquxing on 2016/5/24.
 */
var usr=require('../db/db');
var uuid = require('node-uuid'); //调用uuid模块
var client=usr.connect(); //创建数据库连接
var moment = require('moment');

function getWeibo(userid ,callback){

    client.query('select p.name ,t.id,t.content,t.time from t_weibo t '
    +' left JOIN t_user p'
    +' on t.userid=p.id'
    +' where t.userid=? order by t.time desc',[userid],function(err,results,fields){
        if(err) throw err;
        for(var i = 0; i < results.length; i++)
        {
            results[i].time=moment(results[i].time).format('YYYY MM DD, HH:mm:ss ');

        }
        callback(results);
    });
}

function Insweibo(userid,content ,callback){
    var time =moment(new Date()).format('YYYY-MM-DD, HH:mm:ss ');
    var id=uuid.v4();
    client.query('insert into t_weibo values(?,?,?,?)',[id,content,time,userid],function(err,results,fields){
        if( err ){
            console.log( "error:" + err.message);
            return err;
        }
        callback(err);
    });
}

function getWeiboList( callback){

    client.query('select p.name ,t.id,t.content,t.time from t_weibo t '
        +' left JOIN t_user p'
        +' on t.userid=p.id'
        +' order by t.time desc',function(err,results,fields){
        if(err) throw err;
        for(var i = 0; i < results.length; i++)
        {
            results[i].time=moment(results[i].time).format('YYYY MM DD, HH:mm:ss ');

        }
        callback(results);
    });
}

exports.getWeibo  = getWeibo;
exports.Insweibo  = Insweibo;
exports.getWeiboList  = getWeiboList;